package com.ctshk.rpc.order.localfun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.ChannelMainData;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.localfun.dto.LocalFunPriceRemainderDTO;
import com.ctshk.rpc.localfun.dto.LocalFunProductTicketDTO;
import com.ctshk.rpc.localfun.service.ILocalFunCostPriceService;
import com.ctshk.rpc.localfun.service.ILocalFunProductService;
import com.ctshk.rpc.order.localfun.dto.LocalFunOrderTouristDTO;
import com.ctshk.rpc.order.localfun.entity.LocalFunOrder;
import com.ctshk.rpc.order.localfun.entity.LocalFunOrderTourist;
import com.ctshk.rpc.order.localfun.mapper.LocalFunOrderMapper;
import com.ctshk.rpc.order.localfun.mapper.LocalFunOrderTouristMapper;
import com.ctshk.rpc.order.localfun.req.LocalFunOrderTouristModifyReq;
import com.ctshk.rpc.order.localfun.req.LocalFunOrderTouristReq;
import com.ctshk.rpc.order.localfun.service.ILocalFunOrderTouristService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 门票订单旅客信息 服务实现类
 * </p>
 *
 * @author gzlwansi001
 * @since 2021-02-24
 */
@DubboService
public class LocalFunOrderTouristServiceImpl extends ServiceImpl<LocalFunOrderTouristMapper, LocalFunOrderTourist> implements ILocalFunOrderTouristService {

    @Autowired
    private LocalFunOrderTouristMapper localFunOrderTouristMapper;

    @Autowired
    private LocalFunOrderMapper localFunOrderMapper;

    @DubboReference
    private ILocalFunProductService iLocalFunProductService;

    @DubboReference
    private ILocalFunCostPriceService iLocalFunCostPriceService;

    @Override
    public Result<List<LocalFunOrderTouristDTO>> list(LocalFunOrderTouristReq localFunOrderTouristReq, TokenUser currentUser) {
        QueryWrapper<LocalFunOrderTourist> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("order_id",localFunOrderTouristReq.getOrderId());
        List<LocalFunOrderTourist> list = localFunOrderTouristMapper.selectList(wrapper);
        List<LocalFunOrderTouristDTO> localFunOrderTouristDTOList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)) {
            return Result.success(localFunOrderTouristDTOList);
        }
        for(LocalFunOrderTourist localFunOrderTourist : list){
            LocalFunOrderTouristDTO localFunOrderTouristDTO = EntityUtil.copy(localFunOrderTourist,LocalFunOrderTouristDTO.class);
            localFunOrderTouristDTO.setCostPrice(localFunOrderTourist.getSinglePrice());
            localFunOrderTouristDTO.setCharge(localFunOrderTourist.getCharge());
//            QueryWrapper<LocalFunOrder> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("is_deleted",0);
//            queryWrapper.eq("id",localFunOrderTourist.getOrderId());
//            LocalFunOrder localFunOrder = localFunOrderMapper.selectOne(queryWrapper);
//            if(localFunOrder != null){
//                if(localFunOrder.getSource() == 1){
//                    //获取对应的金额和服务费
//                    LocalFunProductTicketDTO localFunProductTicketDTO = iLocalFunCostPriceService.getLocalFunProductTicketDTO(localFunOrder.getProductTicketsId(),localFunOrder.getProductId(),currentUser.getId(),localFunOrder.getGmtCreate());
//                    if(localFunProductTicketDTO != null){
//                        localFunOrderTouristDTO.setCostPrice(localFunProductTicketDTO.getCostPrice() != null ?
//                                localFunProductTicketDTO.getCostPrice().setScale(2, BigDecimal.ROUND_HALF_UP):null);
//                        localFunOrderTouristDTO.setCharge(localFunProductTicketDTO.getCharge() != null ?
//                                localFunProductTicketDTO.getCharge().setScale(2, BigDecimal.ROUND_HALF_UP):null);
//                    }
//                }else {
//                    String attrIds = localFunOrder.getProductTicketsId();
//                    String[] split = attrIds.split(",");
//                    Long attrId1 = Long.valueOf(split[0]);
//                    Long attrId2 = null;
//                    if(split.length > 1){
//                        attrId2 = Long.valueOf(split[1]);
//                    }
//                    Long channelId = localFunOrder.getSource() == 3?ChannelMainData.CHINA_TRAVEL.getId():ChannelMainData.APP_H5.getId();
//                    LocalFunPriceRemainderDTO localFunSalePriceInfoHandle = iLocalFunProductService.findLocalFunSalePriceInfoHandle(3, localFunOrder.getProductId(), localFunOrder.getGmtCreate().toLocalDate(), channelId, attrId1, attrId2);
//                    localFunOrderTouristDTO.setCostPrice(localFunSalePriceInfoHandle.getCostPrice()
//                            .multiply(localFunSalePriceInfoHandle.getExchangeRate()).setScale(2,BigDecimal.ROUND_HALF_UP));
//                    localFunOrderTouristDTO.setCharge(localFunSalePriceInfoHandle.getReserveCharge());
//                }
//                localFunOrderTouristDTO.setPaymentStatus(localFunOrder.getPaymentStatus());
//            }
            localFunOrderTouristDTOList.add(localFunOrderTouristDTO);
        }

        return Result.success(localFunOrderTouristDTOList);
    }

    @Override
    public Result modify(LocalFunOrderTouristModifyReq localFunOrderTouristModifyReq, TokenUser tokenUser) {
        LocalFunOrderTourist localFunOrderTourist = localFunOrderTouristMapper.selectById(localFunOrderTouristModifyReq.getId());
        if(localFunOrderTourist != null){
            localFunOrderTourist.setSurname(localFunOrderTouristModifyReq.getSurname());
            localFunOrderTourist.setName(localFunOrderTouristModifyReq.getName());
            localFunOrderTourist.setTouristName(localFunOrderTouristModifyReq.getSurname()+localFunOrderTouristModifyReq.getName());
            localFunOrderTourist.setTouristType(localFunOrderTouristModifyReq.getTouristType());
            localFunOrderTourist.setBirthday(localFunOrderTouristModifyReq.getBirthday());
            localFunOrderTourist.setTouristType(localFunOrderTouristModifyReq.getTouristType());
            localFunOrderTourist.setCertificatesNumber(localFunOrderTouristModifyReq.getCertificatesNumber());
            localFunOrderTourist.setVaildDate(localFunOrderTouristModifyReq.getVaildDate());
            localFunOrderTourist.setCertificatesTypeId(Long.parseLong(localFunOrderTouristModifyReq.getCertificatesType().getValue()));
            localFunOrderTourist.setCertificatesType(localFunOrderTouristModifyReq.getCertificatesType().getLabel());
            localFunOrderTourist.setGmtModified(LocalDateTime.now());
            localFunOrderTourist.setModifiedId(tokenUser.getId());
            localFunOrderTouristMapper.updateById(localFunOrderTourist);
        }
        return Result.success(localFunOrderTourist.getId());
    }
}
